package com.example.beson.androiddemo.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.beson.androiddemo.R;
import com.example.beson.androiddemo.login.mvp.view.MainFragmentView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 史国院 on 2017/4/26.
 * Desc ******
 */

public class MainFragment extends Fragment implements MainFragmentView{
    ViewPager viewpagerLayout;
    TabLayout mainTabLayout;
    private FragmentPagerAdapter fragmentPagerAdapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.main_fragment,container,false);
        initUI(view);
        return view;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void dismissLoading() {

    }

    @Override
    public void showMsg(String msg) {

    }

    @Override
    public void getData() {

    }

    public void initUI(View compatActivity) {
        viewpagerLayout= (ViewPager) compatActivity.findViewById(R.id.main_viewpager);
        mainTabLayout= (TabLayout) compatActivity.findViewById(R.id.main_tablayout);
        initViewpager();
        initFragment();
    }

    @Override
    public Context getContext() {
        return getActivity();
    }
    public void initViewpager(){
        mainTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mainTabLayout.setSelectedTabIndicatorColor(Color.parseColor("#00FF00"));
        mainTabLayout.setSelectedTabIndicatorHeight(5);
        mainTabLayout.setTabTextColors(Color.parseColor("#666666"),Color.parseColor("#00FF00"));
        mainTabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
    }
    String[] titles;
    List<Fragment> fraglist;
    public void initFragment(){
        titles=new String[]{"Android","IOS","PHP","Java","IOS","PHP","Java"};
        fraglist=new ArrayList<>();
        fraglist.add(new AndroidFragment());
        fraglist.add(new IosFragment());
        fraglist.add(new PhpFragment());
        fraglist.add(new JavaFragment());
        fraglist.add(new IosFragment());
        fraglist.add(new PhpFragment());
        fraglist.add(new JavaFragment());
        fragmentPagerAdapter=new fAdapter(getChildFragmentManager());
        viewpagerLayout.setAdapter(fragmentPagerAdapter);
        mainTabLayout.setupWithViewPager(viewpagerLayout);
    }
    class fAdapter extends FragmentPagerAdapter{

        public fAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fraglist.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if(position>=0&&position<titles.length){
                return titles[position];
            }
            return null;
        }

        @Override
        public int getCount() {
            return fraglist.size();
        }
    }

}
